package AdminTab;

import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class KWarchive {
	static WebDriver myD; 
	
	

	

	
	

	
	public static String verifyText(FirefoxDriver driver, String xPath, String vText) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: verifies text from any field.
		//i/p: driver, xPath
		//o/p:  Data
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		if (vText.equalsIgnoreCase(driver.findElement(By.xpath(xPath)).getText())){  //returns whatever comes here
			
			return "pass"; 
			
		} else {
			return "Fail";
		}
		
		
	}
	

	
	
	public static void clickElement(FirefoxDriver driver, String xPath) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: Click any element on the AUT
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.xpath(xPath)).click(); // sometimes click may not work, so I write a code to send enter
		//driver.findElement(By.linkText(linkText)).click();
		//driver.findElement(By.xpath(xPath)).sendKeys("\n");  //\n is enter key
		
	}
		
	public static void clickElementbylink(FirefoxDriver driver, String link) {  //B'coz im going to get an o/p, I changed void to String to return the data
		// Purpose: Click any element on the AUT
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
		driver.findElement(By.linkText(link)).click();
		driver.findElement(By.linkText(link)).sendKeys("\n");
		
	}
	
	public static void quitBrowser(FirefoxDriver driver){
		// Purpose: Navigates a browser to a URL
					//i/p: driver, Data
					//o/p:  NIl
					//CreatedBy : Priya
					//When :8/05/14
					//EditedBy : Priya
					//When : 8/5/14
		
		driver.quit();
		
	}
		
	
		public static void selectList(FirefoxDriver driver, String xPath, String Data) { 
			// Purpose: Select from a drop down list 
			//i/p: driver, xPath, Data
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		Select myList = new Select(driver.findElement(By.xpath(xPath)));//creating memory in myList
		myList.selectByVisibleText(Data);
		myList = null;  //clears the memory in my list
			
		}
			
			
	
		public static void checkBox(FirefoxDriver driver, String xPath) {  //this has to be checked.
			// Purpose: checks a checkbox
			//i/p: driver, xPath
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		//Is it checked?
			if(driver.findElement(By.xpath(xPath)).getAttribute("checked").equalsIgnoreCase("checked")){
				//then do nothing. get attribute gets something from the page such as the value of the attribute
		
			} else {
			//click
			driver.findElement(By.xpath(xPath)).click();
			}		
		}

	//uncheckBox
		

		public static void uncheckBox(FirefoxDriver driver, String xPath) {  //this has to be checked.
			// Purpose: unchecks a checkbox
			//i/p: driver, xPath
			//o/p:  NIl
			//CreatedBy : Priya
			//When :8/05/14
			//EditedBy : Priya
			//When : 8/5/14
			
		//Is it checked?
			if(driver.findElement(By.xpath(xPath)).isSelected()){
				//then click
				driver.findElement(By.xpath(xPath)).click();
				System.out.println("Now unchecked");
			} else {
			System.out.println("Already checked");
	//do nothing
			}		
		}

		
		
	// selectRadio
	
	public static void navigateBrowser(FirefoxDriver driver, String Data){
		// Purpose: Navigates a browser to a URL
					//i/p: driver, Data
					//o/p:  NIl
					//CreatedBy : Priya
					//When :8/05/14
					//EditedBy : Priya
					//When : 8/5/14
		
		driver.navigate().to(Data);
		
	}

	public static void waitTime(long lTime) throws Exception{
		// Purpose: it will wait for a certain amount of time.
					//i/p: time in milli seconds
					//o/p:  NIl
					//CreatedBy : Priya
					//When :8/05/14
					//EditedBy : Priya
					//When : 8/5/14
		
		Thread.sleep(lTime);
		
	}
	
	
	public static void dropDown(FirefoxDriver driver, String xPath, int sIndex) { 
		// Purpose: Select from a drop down list 
		//i/p: driver, xPath, Data
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
			WebElement myE = driver.findElement(By.xpath(xPath)); //decalring a web element
		    Select mylist = new Select(myE); // Pointing to a WE object which is of type Select
		    mylist.selectByIndex(sIndex); //By index is poiting to order in the drop-down list
		    
	}
	
	public static void checkBox2(FirefoxDriver driver, String xPath) {  //this has to be checked.
		// Purpose: checks a checkbox
		//i/p: driver, xPath
		//o/p:  NIl
		//CreatedBy : Priya
		//When :8/05/14
		//EditedBy : Priya
		//When : 8/5/14
		
	//Is it checked?
		if(driver.findElement(By.xpath(xPath)).isSelected()){
			//then click
			
			System.out.println("checked");
		} else {
		driver.findElement(By.xpath(xPath)).click();
		System.out.println("Now checked");

		}		
	}

public static String defaultvaluelist(FirefoxDriver driver, String xPath, String Data){
		
		String Appvalue = driver.findElement(By.xpath(xPath)).getText();
	
		if (Data.equalsIgnoreCase(Appvalue)){
			return "pass";
		} else {

		 //return "Fail: The Default value is incorrect";
				return "Fail: the Default value does not match on the console.";
				
		}
		
	
	}

public static void alertBoxaccept(FirefoxDriver driver) {
	Alert myAlert = driver.switchTo().alert();
	myAlert.accept();
}
public static void alertBoxdismiss(FirefoxDriver driver) {
	Alert myAlert = driver.switchTo().alert();
	myAlert.dismiss();
}

	 public static void list_select_value(FirefoxDriver driver, String xpath, String Data){
		 //Purpose : Gives the total elements in the drop down list
		 
		  WebElement myV = driver.findElement(By.xpath(xpath));
		 java.util.List<WebElement> myL =  myV.findElements(By.tagName("option")) ; //List is an array . when I use tag name, I can say the tag name of that elemenst are option
	  	//The above line will list all the elements
		 System.out.println("size of items in drop down is" + myL.size());
		 for(int i=0; i<myL.size(); i++){
			 System.out.println("Value in the item" + i + "is" + myL.get(i).getText());
			 if (myL.get(i).getText().equalsIgnoreCase(Data)){
				 myL.get(i).click();
			 }
}
	 }
}
